package com.nuanshui.heatedloan.userinfo.service;

import com.nuanshui.heatedloan.baseservice.service.CommonServiceImpl;
import com.nuanshui.framework.consts.TransactionManagerType;
import com.nuanshui.framework.springContext.SpringContextUtil;
import com.nuanshui.framework.utils.UserAuthUtil;
import com.nuanshui.heatedloan.userinfo.dal.mapper.UserAuthMapper;
import com.nuanshui.heatedloan.userinfo.facade.UserAuthService;
import com.nuanshui.heatedloan.userinfo.model.SimpleUserInfo;
import com.nuanshui.heatedloan.userinfo.model.UserAuth;
import com.nuanshui.heatedloan.userinfo.tool.UserTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

/**
 * 用户权限处理实现类
 * @ClassName: UserAuthServiceImpl 
 * @author guohao
 * @date 2016年2月22日 上午9:50:06 
 *
 */
@Service("userAuthService")
@Transactional(readOnly=true,value=TransactionManagerType.USER_INFO_TRANSACTION_MANAGER)
public class UserAuthServiceImpl extends CommonServiceImpl implements UserAuthService{

	public enum UserAuthOperation
	{
		Add,
		Del
	}

	@Autowired
	private UserAuthMapper userAuthMapper;

	@Autowired
	private UserTool userTool;

	@Override
	@Transactional(value=TransactionManagerType.USER_INFO_TRANSACTION_MANAGER)
	public void addAuth(String userId, Integer authType) {
		doUserAuth(userId,authType,UserAuthOperation.Add);
	}

	@Override
	@Transactional(value=TransactionManagerType.USER_INFO_TRANSACTION_MANAGER)
	public void delAuth(String userId, Integer authType) {
		doUserAuth(userId,authType,UserAuthOperation.Del);
	}

	@Transactional(value=TransactionManagerType.USER_INFO_TRANSACTION_MANAGER)
	public void doUserAuth(String userId, Integer authType,UserAuthOperation operation) {
		Long auth =userAuthMapper.getUserAuth(userId);

		if(operation==UserAuthOperation.Add)
		{
			auth = UserAuthUtil.addAuth(auth, authType);
		}
		else if(operation==UserAuthOperation.Del)
		{
			auth = UserAuthUtil.delAuth(auth, authType);
		}

		internalUpdateUserAuth(userId, auth);
	}

	@Transactional(value=TransactionManagerType.USER_INFO_TRANSACTION_MANAGER)
	private void internalUpdateUserAuth(String userId,Long auth) {
		userAuthMapper.updateUserAuth(userId, auth);
		SimpleUserInfo simpleUserInfo =userTool.getSimpleUserInfo(userId);
		if(simpleUserInfo!=null) {
			simpleUserInfo.setAuth(auth);
			userTool.setSimpleUserInfo(simpleUserInfo);
		}
	}

	@Override
	public boolean hasAuthByCurrentUser(int authId)
	{
		String currentId = null;
		try {
			currentId = userTool.getCurrentUser().getId();
		} catch (Exception e) {
			return  false;
		}

		return hasAuthByUserId(authId,currentId);
	}

	@Override
	public boolean hasAuthByUserId(int authId,String currentId)
	{
		boolean isAuth=false;
		if(!StringUtils.isEmpty(currentId))
		{
			UserTool commonTool = SpringContextUtil.getBean(UserTool.class);
			SimpleUserInfo usrInfo=commonTool.getSimpleUserInfo(currentId);
			isAuth=UserAuthUtil.hasAuth(usrInfo.getAuth(),authId);

			//isTestUser=(isTestUser(currentId)>0);
		}

		return isAuth;
	}

	@Override
	public Integer isAdministrator(String userId) {
		Long auth = userAuthMapper.getUserAuth(userId);
		boolean isAdmin = UserAuthUtil.hasAuth(auth, 3);

		return isAdmin ? 1 : 0;
	}

	/**
	 * 更新用户的权限
	 * @param type
	 * @param value
	 */
	@Override
	@Transactional(value=TransactionManagerType.USER_INFO_TRANSACTION_MANAGER)
	public void updateUserAuth(String type,Integer value) {
		//User user = getCurrentUser();
		userAuthMapper.updateUserOperationAuth(type, value,userTool.getCurrentUser().getId());
	}

	@Override
	@Transactional(value=TransactionManagerType.USER_INFO_TRANSACTION_MANAGER)
	public UserAuth loadUserAuth(String userId) {
		UserAuth userAuth = userAuthMapper.get(userId);
		if(userAuth == null) {
			userAuth = new UserAuth();
			userAuth.setUserId(userId);
			userAuth.setLike(1);
			userAuth.setPost(1);
			userAuth.setTag(1);
			userAuth.setRemind(1);
			userAuth.setReply(1);
			userAuth.setReplyUser(1);
			userAuth.setSearchCertificate(0);
			userAuth.setInterested(1);
			userAuthMapper.save(userAuth);
		}else {
			return userAuth;
		}

		return userAuthMapper.get(userId);
	}
}
